<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>编辑作品 - {$config_sitename}</title>
    <link rel="stylesheet" href="__CSS__/global.css" />
    <link href="{$config_siteurl}static/js/artDialog/skins/default.css" rel="stylesheet" />
    <link rel="stylesheet" href="__CSS__/yf.css" />
    <link rel="stylesheet" type="text/css" href="__COMMJS__/yf_file/plupload/common.css" />
    <script type="text/javascript">
        //全局变量
        var GV = {
            siteUrl: "__ROOT__/",
            JS_ROOT: "__COMMJS__/"
        };
    </script>
    <script src="__COMMJS__/wind.js"></script>
    <script src="__JS__/jquery.min.js"></script>
</head>

<body>

<div class="b-page">
    <Admintemplate file="Home/Public/header"/>
    <div class="b-login">
        <div class="b-body l-center yf-b-body">
            <div class="b-login__top">
                <!-- <a href="javascript:;" class="b-login__tab act">编辑烘焙作品</a>-->
                <p class="yf-main-title">编辑烘焙作品<span> —— 从现在起，不做孤独的美食家</span></p>
            </div>
            <div class="bake_body">
                <form name="form1" class="ajaxForm" action="__WEBURL__/user/bake/worksedit" method="post">
                    <div class="create yf_create">
                        <ul>
                            <li><select name="cateid">{$category}</select></li>
                            <li><input type="text" class="create_input yf_create_ta" size="80" name="title" placeholder="点击添加作品名称" style="border-radius: 5px;" value="{$data.title}" /></li>
                            <li class="yf_created_title" style="display:none;">作品主图(图片不能小于600*400px)</li>
                            <li class="mainpic yf-mainpic2">
                             	 <ul class="yf-mainpic-list">
                             	 <foreach name="data.all_pic" item="pic">
                             	 	<if condition="$key eq 0">
                            	 	<Form function="yf_image" parameter="image,image,$pic,works"/>
                            	 	</if>
                            	 	<if condition="$key neq 0">
                            	 	<li class="yf-img-move"><img src="{$pic}"><div class="yf-move-window yf-move-del"><span>删除</span></div><input type="hidden" name="all_pic[]" value="{$pic}"></li>
                            	 	</if>
                            	 </foreach>
                            	 	<Form function="yf_image_ug" parameter="image,image,'',works"/>
                            	 </ul>
                          	</li>
                            <li class="yf_created_desc">简介</li>
                            <li><textarea name="content" cols="30" rows="10" class="create_ta yf_create_ta" style="height: 163px;" placeholder="这个作品背后的故事~（必填）">{$data.content}</textarea></li>
                            <li class="yf_created_mt">用料</li>
                            <li>
                                <div class="mt_list yf_mt_list">
                                	<empty name="data.works_materials">
                                    <ul>
                                        <li class="yf_first">
                                        	<input type="text" name="mname[]" class="mtinput yf_mtinput_one" placeholder="如:低筋面粉"/>
                                        </li>
                                        <li class="yf_second">
                                        	<input type="text" name="mvalue[]" class="mtinput yf_mtinput_two" placeholder="如:250g"/>
                                        </li>
                                        <li>
                                        	<span class="yf_mtinput_three" title="删除" data-id="0">&times;</span>
                                        </li>
                                    </ul>
                                    <ul>
                                        <li class="yf_first">
                                        	<input type="text" name="mname[]" class="mtinput yf_mtinput_one" placeholder="如:白砂糖"/>
                                        </li>
                                        <li class="yf_second">
                                        	<input type="text" name="mvalue[]" class="mtinput yf_mtinput_two" placeholder="如:21g"/>
                                        </li>
                                        <li>
                                        	<span class="yf_mtinput_three" title="删除" data-id="0">&times;</span>
                                        </li>
                                    </ul>
                                    <ul>
                                        <li class="yf_first">
                                        	<input type="text" name="mname[]" class="mtinput yf_mtinput_one" placeholder=""/>
                                        </li>
                                        <li class="yf_second">
                                        	<input type="text" name="mvalue[]" class="mtinput yf_mtinput_two" placeholder=""/>
                                        </li>
                                        <li>
                                        	<span class="yf_mtinput_three" title="删除" data-id="0">&times;</span>
                                        </li>
                                    </ul>
                                    <ul>
                                        <li class="yf_first">
                                        	<input type="text" name="mname[]" class="mtinput yf_mtinput_one" placeholder=""/>
                                        </li>
                                        <li class="yf_second">
                                        	<input type="text" name="mvalue[]" class="mtinput yf_mtinput_two" placeholder=""/>
                                        </li>
                                        <li>
                                        	<span class="yf_mtinput_three" title="删除" data-id="0">&times;</span>
                                        </li>
                                    </ul>
                                    <ul>
                                        <li class="yf_first">
                                        	<input type="text" name="mname[]" class="mtinput yf_mtinput_one" placeholder=""/>
                                        </li>
                                        <li class="yf_second">
                                        	<input type="text" name="mvalue[]" class="mtinput yf_mtinput_two" placeholder=""/>
                                        </li>
                                        <li>
                                        	<span class="yf_mtinput_three" title="删除" data-id="0">&times;</span>
                                        </li>
                                    </ul>
                                    <ul>
                                        <li class="yf_first">
                                        	<input type="text" name="mname[]" class="mtinput yf_mtinput_one" placeholder=""/>
                                        </li>
                                        <li class="yf_second">
                                        	<input type="text" name="mvalue[]" class="mtinput yf_mtinput_two" placeholder=""/>
                                        </li>
                                        <li>
                                        	<span class="yf_mtinput_three" title="删除" data-id="0">&times;</span>
                                        </li>
                                    </ul>
                                    </empty>
                                    <foreach name="data.works_materials" item="wm">
                                    <ul>
                                        <li class="yf_first">
                                        	<input type="hidden" name="materials_id[]" value="{$wm.id}" />
                                        	<input type="text" name="mname[]" class="mtinput yf_mtinput_one" value="{$wm.mname}" placeholder="如:低筋面粉"/>
                                        </li>
                                        <li class="yf_second">
                                        	<input type="text" name="mvalue[]" class="mtinput yf_mtinput_two" value="{$wm.mvalue}" placeholder="如:250g"/>
                                        </li>
                                        <li>
                                        	<span class="yf_mtinput_three" title="删除" data-id="{$wm.id}">&times;</span>
                                        </li>
                                    </ul>
                                    </foreach>
                                    <div class="yf_clear"></div>
                                <!--     <a id="add_mt" class="btn">+添加用料</a>-->
                                	<a id="add_mt" class="yf_add_mt">增加一栏</a>
                                </div>
                            </li>
                            <li class="yf_created_step">步骤<Form function="yf_image_all" parameter="image,image,'',works"/><span class="yf_addopt_tip">(按住Crtl可多选,为保证您的使用体验,单次上传请不要超过9张)</span></li>
                            <li id="opt" class="opt" style="width: 715px;">
                            	<empty name="data.works_makestep">
                            		<ul class='yf_opt'>
	                            		<li class='yf_opt_one'>
	                            			<Form function="yf_image_pr" parameter="simage[],image,'',works"/>
	                            		</li>
	                            		<li class='yf_opt_two'>
	                            			<p class='yf_opt_num'></p>
	                            			<textarea cols='60%' rows='10' name='scontent[]'></textarea>
	                            		</li>
	                            		<li class='yf_opt_three'>
	                            			<span class='yf_opt_up' title='上移一步'></span>
	                            			<span class='yf_opt_down' title='下移一步'></span>
	                            			<span class='yf_opt_add' title='插入'></span>
	                            			<span class='yf_opt_del' title='删除' data-id='0'></span>
	                            		</li>
	                            		<div class='yf_clear'></div>
	                            	</ul>
	                            	<ul class='yf_opt'>
	                            		<li class='yf_opt_one'>
	                            			<Form function="yf_image_pr" parameter="simage[],image,'',works"/>
	                            		</li>
	                            		<li class='yf_opt_two'>
	                            			<p class='yf_opt_num'></p>
	                            			<textarea cols='60%' rows='10' name='scontent[]'></textarea>
	                            		</li>
	                            		<li class='yf_opt_three'>
	                            			<span class='yf_opt_up' title='上移一步'></span>
	                            			<span class='yf_opt_down' title='下移一步'></span>
	                            			<span class='yf_opt_add' title='插入'></span>
	                            			<span class='yf_opt_del' title='删除' data-id='0'></span>
	                            		</li>
	                            		<div class='yf_clear'></div>
	                            	</ul>
	                            	<ul class='yf_opt'>
	                            		<li class='yf_opt_one'>
	                            			<Form function="yf_image_pr" parameter="simage[],image,'',works"/>
	                            		</li>
	                            		<li class='yf_opt_two'>
	                            			<p class='yf_opt_num'></p>
	                            			<textarea cols='60%' rows='10' name='scontent[]'></textarea>
	                            		</li>
	                            		<li class='yf_opt_three'>
	                            			<span class='yf_opt_up' title='上移一步'></span>
	                            			<span class='yf_opt_down' title='下移一步'></span>
	                            			<span class='yf_opt_add' title='插入'></span>
	                            			<span class='yf_opt_del' title='删除' data-id='0'></span>
	                            		</li>
	                            		<div class='yf_clear'></div>
	                            	</ul>
                            	</empty>
                            	<foreach name="data.works_makestep" item="ws">
                            		<ul class='yf_opt'>
                            			<li class='yf_opt_one'>
                            				<Form function="yf_image_pr" parameter="simage[],image,$ws[simage],works"/>
                            			</li>
                            			<li class='yf_opt_two'>
                            				<p class='yf_opt_num'></p>
                            				<textarea cols='60%' rows='10' name='scontent[]'>{$ws.scontent}</textarea>
                            			</li>
                            			<li class='yf_opt_three'>
                            				<span class='yf_opt_up' title='上移一步'></span>
                            				<span class='yf_opt_down' title='下移一步'></span>
                            				<span class='yf_opt_add' title='插入'></span>
                            				<span class='yf_opt_del' title='删除' data-id="{$ws.id}"></span>
                            			</li>
                            			<div class='yf_clear'></div>
                            		</ul>
                            	</foreach>
                            </li>
                            <li class="yf_opt_btn"><a id="addopt" class="yf_add_ot">增加一步</a></li>
                            <li>小贴士</li>
                            <li><textarea name="note" class="create_ta yf_create_ta" rows="5" style="height: 113px;" placeholder="必填">{$data.note}</textarea></li>
                            <li></li>
                            <li></li>
                        </ul>
                    </div>
					<input type="hidden" name="status" value="{$data.status}">
                    <input type="hidden" name="workid" value="{$data.workid}"/>
                    <div class="yf-btn-background">
                    	<div class="yf-btn-back">
                    		<button class="create_btn Ajax_submit_btn" >发布</button>
                    		<button type="button" class="create_btn yf-post" data-type="yes" style="margin-left: 5px;">存草稿</button>
                    	</div>
					</div>
                </form>

            </div>
        </div>
    </div>

    <Admintemplate file="Home/Public/footer"/>

</div>
<div class="up-img">
            
</div>
<script src="__JS__/index.js"></script>
<script src="__JS__/jquery.common.js"></script>
<script src="__JS__/jquery.form.min.js"></script>
<script src="__JS__/jquery.doajax.js"></script>

<script src="__COMMJS__/common.js"></script>
<script src="__COMMJS__/yf.js"></script>
<script type="text/javascript" src="__COMMJS__/yf_upload/jquery.ui.widget.js"></script>
<script type="text/javascript" src="__COMMJS__/yf_upload/jquery.iframe-transport.js"></script>
<script type="text/javascript" src="__COMMJS__/yf_upload/jquery.fileupload.js"></script>
<script src="__COMMJS__/yf_file/plupload/plupload.full.min.js"></script>
<script src="__COMMJS__/yf_file/yf-file.js"></script>

<script>
    $(document).ready(function() {
		
		function get_html()
		{
			var html = '';
			html += "<ul class='yf_opt'>";
			html += "<li class='yf_opt_one'>";
			html += "<Form function="yf_image_pr" parameter="simage[],image,'',works"/>";
			html += "</li>";
			html += "<li class='yf_opt_two'>";
			html += "<p class='yf_opt_num'></p>";
			html += "<textarea cols='60%' rows='10' name='scontent[]'></textarea>";
			html += "</li>";
			html += "<li class='yf_opt_three'>";
			html += "<span class='yf_opt_up' title='上移一步'></span>";
			html += "<span class='yf_opt_down' title='下移一步'></span>";
			html += "<span class='yf_opt_add' title='插入'></span>";
			html += "<span class='yf_opt_del' title='删除' data-id='0'></span>";
			html += "</li>";
			html += "<div class='yf_clear'></div>";
			html += "</ul>";
			return html;
		}
    	var old_img = '';
    	$('.yf-fileupload').fileupload({
 	    	dataType: 'json',
 	        add: function (e, data) {
 	        	old_img = $(this).parent().find('img').attr('src');
 	        	$(this).parent().find('.yf-move-window').hide();
 	 	        $(this).parent().find('img').attr('src', '/static/images/load2.gif');
 	            data.submit();
 	        },
 	        done: function (e, data) {
 	        	var info = data.result.content;
	 	        if(data.result.error == 0)
	 	        {
		 	        var path = info.path + "yf_" + info.savename;
			 	    $(this).parent().find('img').attr('src', path);
				 	$(this).parent().find('.yf-img-value').val(path);
	 	        }
	 	        else
	 	        {
	 	        	$.tipMessage(info, 2, 3000);
			 	    //$(this).parent().find('img').attr('src', '/static/images/icon/upload-pic2.png');
	 	        	$(this).parent().find('img').attr('src', old_img);
	 	        }
 	        }
 	    });
    	
   		$('.yf_update').click(function(){
	        var title = $(this).attr('data-title');
	        var url = $(this).attr('data-url');
	        yf_file_star($(this), title, url);
	    });
    	
    	$('.yf-mainpic2').on({ 
		    mouseenter: function() { 
		    	$(this).find('.yf-move-window').show();
		    }, 
		    mouseleave: function() { 
		    	$(this).find('.yf-move-window').hide();
		    }
		}, '.yf-img-move');

		$('.yf-mainpic2').on('click', '.yf-move-del', function(){
			$(this).parent().remove();
		});

		$('#add_mt').click(function(e){
			e.preventDefault();
			var _li_html='<ul><li class="yf_first"><input type="text" name="mname[]" class="mtinput yf_mtinput_one" placeholder=""/></li><li class="yf_second"><input type="text" name="mvalue[]" class="mtinput yf_mtinput_two" placeholder=""/></li><li><span class="yf_mtinput_three" title="删除" data-id="0">&times;</span></li></ul>';
			$(_li_html).insertAfter('.yf_mt_list > ul:last');
		});

		$('.yf_mt_list').on('click', '.yf_mtinput_three', function(e){
			e.preventDefault();
			var len = $('.yf_mtinput_three').length;
			if(len == 1)
			{
				$.tipMessage("至少要填写一种用料", 2, 3000);
			}
			else
			{
	            var id=$(this).attr('data-id'),
	            _this=$(this);console.log(id);
	            if(id=='0'){
	            	_this.parent().parent().remove();
	                return false;
	            }
	            $.getJSON(GV.siteUrl + 'user/bake/delworkMT?id='+id).done(function (data) {
	                if (data.status == true) {
	                	_this.parent().parent().remove();
	                    return false;
	                } else if (data.status == false) {
	                    $.tipMessage(data.info, 1, 3000);
	                    return false;
	                }
	            });
			}
		});

		$('#opt').on({
			mouseenter: function() { 
	        	$(this).find('.yf_opt_three').show();
		    }, 
		    mouseleave: function() { 
		    	$(this).find('.yf_opt_three').hide();
	    	}
		}, '.yf_opt');

		$('#opt').on('click', '.yf_opt_del', function(e){
			e.preventDefault();
			var len = $('.yf_opt_del').length;
			if(len == 1)
			{
				$.tipMessage("至少要填写一个步骤", 2, 3000);
			}
			else
			{
				var id=$(this).attr('data-id'),
                _this=$(this);
	            if(id=='0'){
	            	_this.parent().parent().remove();
					up_set_num();
	                return false;
	            }
	            $.getJSON(GV.siteUrl + 'user/bake/delworkST?id='+id).done(function (data) {
	                if (data.status == true) {
	                	_this.parent().parent().remove();
						up_set_num();
	                    return false;
	                } else if (data.status == false) {
	                    $.tipMessage(data.info, 1, 3000);
	                    return false;
	                }
	            });
			}
		});

		$('#opt').on('click', '.yf_opt_add', function(){
			var _parent = $(this).parent().parent();
			var html = get_html();
			$(html).insertBefore(_parent);
			yf_start();
			up_set_num();
		});

		$('.yf_add_ot').click(function(){
			var html = get_html();
			$(html).appendTo('#opt');
			yf_start();
			up_set_num();
		});

		$('#opt').on('click', '.yf_opt_up', function(){
			var num = $('.yf_opt_up').index(this);
			num = parseInt(num) + 1;
			if(num == 1)
			{
				$.tipMessage("这是第一步，不能上移", 2, 3000);
				return false;
			}
			var now_this = $(this).parent().parent();
			var now_img = now_this.find('.yf_opt_one > .yf-img-value').val();
			var now_text = now_this.find('.yf_opt_two > textarea').val();
			var now_pic = now_this.find('.yf_opt_one > img').attr('src');
			
			var prev_this = now_this.prev();
			var prev_img = prev_this.find('.yf_opt_one > .yf-img-value').val();
			var prev_text = prev_this.find('.yf_opt_two > textarea').val();
			var prev_pic = prev_this.find('.yf_opt_one > img').attr('src');
			
			now_this.find('.yf_opt_one > .yf-img-value').val(prev_img);
			now_this.find('.yf_opt_two > textarea').val(prev_text);
			now_this.find('.yf_opt_one > img').attr('src', prev_pic);
			
			prev_this.find('.yf_opt_one > .yf-img-value').val(now_img);
			prev_this.find('.yf_opt_two > textarea').val(now_text);
			prev_this.find('.yf_opt_one > img').attr('src', now_pic);
		});

		$('#opt').on('click', '.yf_opt_down', function(){
			var num = $('.yf_opt_down').index(this);
			num = parseInt(num) + 1;
			var len = $('.yf_opt_down').length;
			if(num == len)
			{
				$.tipMessage("这是最后一步，不能下移", 2, 3000);
				return false;
			}
			var now_this = $(this).parent().parent();
			var now_img = now_this.find('.yf_opt_one > .yf-img-value').val();
			var now_text = now_this.find('.yf_opt_two > textarea').val();
			var now_pic = now_this.find('.yf_opt_one > img').attr('src');
			
			var prev_this = now_this.next();
			var prev_img = prev_this.find('.yf_opt_one > .yf-img-value').val();
			var prev_text = prev_this.find('.yf_opt_two > textarea').val();
			var prev_pic = prev_this.find('.yf_opt_one > img').attr('src');
			
			now_this.find('.yf_opt_one > .yf-img-value').val(prev_img);
			now_this.find('.yf_opt_two > textarea').val(prev_text);
			now_this.find('.yf_opt_one > img').attr('src', prev_pic);
			
			prev_this.find('.yf_opt_one > .yf-img-value').val(now_img);
			prev_this.find('.yf_opt_two > textarea').val(now_text);
			prev_this.find('.yf_opt_one > img').attr('src', now_pic);
		});

		$('.yf_addopt_btn').click(function(){
	    	var title = $(this).attr('data-title');
	        var url = $(this).attr('data-url');
	        yf_file_star($(this), title, url);
		});

		function up_set_num()
		{
			var len = $('.yf_opt').length;
			$('.yf_opt_num').each(function(i){
				var num = parseInt(i) + 1;
				$(this).text(num);
			});
		}

		up_set_num();

		function yf_start()
		{
			$('.yf-fileupload-good').fileupload({
	 	    	dataType: 'json',
	 	        add: function (e, data) {
	 	 	        $(this).parent().parent().find('img').attr('src', '/static/images/load2.gif');
	 	            data.submit();
	 	        },
	 	        done: function (e, data) {
	 	        	var info = data.result.content;
	 	        	console.log(data);
		 	        if(data.result.error == 0)
		 	        {
			 	        var path = info.path + "yf_" + info.savename;
					 	$(this).parent().find('img').attr('src', path);
						$(this).parent().find('.yf-img-value').val(path);
		 	        }
		 	        else
		 	        {
		 	        	$.tipMessage(info, 2, 3000);
				 	    $(this).parent().find('img').attr('src', '/static/images/icon/yf_upload2.png');
		 	        }
	 	        }
	 	    });
		}
		yf_start();

		$('.yf-post').click(function(){
			var data = {};
			var data_arr = $('form').serialize().replace(/\+/g,' ').split('&');
			for(var i=0;i<data_arr.length;i++)
			{
				var temp=data_arr[i].split('=');
				if(typeof(data[hungry.util.utf8_decode(temp[0])])!='undefined')
				{
					var arr=[];
					if(data[hungry.util.utf8_decode(temp[0])] instanceof Array)
					{
						arr=data[hungry.util.utf8_decode(temp[0])];
					}
					else
					{
						arr.push(data[hungry.util.utf8_decode(temp[0])]);
					}
						arr.push(hungry.util.utf8_decode(temp[1]));
						data[hungry.util.utf8_decode(temp[0])]=arr;
				}
				else
				{
					data[hungry.util.utf8_decode(temp[0])]=hungry.util.utf8_decode(temp[1]);
				}
			}
			var url = "__WEBURL__/user/bake/workstempedit";
			var _this = $(this);
			var type = _this.attr('data-type');
			if(type == 'yes')
			{
				_this.attr('data-type', 'no');
				_this.text('存草稿中...');
				$.post(url, data, function(res){
					if(res.status == 1)
					{
						$.tipMessage(res.info, 1, 3000, 0, function(){
							location.href = res.url;
						});
					}
					else
					{
						$.tipMessage(res.info, 2, 3000);
					}
					_this.attr('data-type', 'yes');
					_this.text('存草稿');
				}, 'json');
			}
		});
    });
</script>

</body>
</html>